package com.baihui.crm;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

public class InsertRecords {
    public static void main(String a[]) {
        try {
            //----------------------------Fetch Ticket ----------------------
            String ticket = getIAMTicket("BaihuiCRM", "baihuitaobaotp", "baihui888");
            String apikey = "3ktH8aeKyypG30KonuRQL7OtZZOftRM6"; //your API key
            String newFormat = "1";

            String targetURL = "https://crm.baihui.com/crm/private/xml/SalesOrders/insertRecords";
//            String targetURL = "https://crm.baihui.com/crm/private/xml/Leads/insertRecords";
            String paramname = "content";
//            String xmlDataString = "<Leads><row no=\"1\"><FL val=\"Company\">Web Download</FL><FL val=\"Last Name\">Scott</FL><FL val=\"Email\">test@test.com</FL><FL val=\"Phone\">1234567890</FL><FL val=\"Contact Owner\">steve</FL><FL val=\"Description\">test</FL></row></Leads>";
//            String xmlDataString = "<SalesOrders><row no=\"1\"><FL val=\"Subject\">Zillium - SO</FL><FL val=\"Due Date\">2009-03-10</FL><FL val=\"Sub Total\">48000.0</FL><FL val=\"Tax\">0.0</FL><FL val=\"Adjustment\">0.0</FL><FL val=\"Grand Total\">48000.0</FL><FL val=\"Billing Street\">test</FL><FL val=\"Shipping Street\">test</FL><FL val=\"Billing City\">test</FL><FL val=\"Shipping City\">test</FL><FL val=\"Billing State\">test</FL><FL val=\"Shipping State\">test</FL><FL val=\"Billing Code\">223</FL><FL val=\"Shipping Code\">223</FL><FL val=\"Billing Country\">test</FL><FL val=\"Shipping Country\">test</FL><FL val=\"Product Details\"><product no=\"1\"><FL val=\"Product Id\">28603000000032545</FL><FL val=\"Unit Price\">10.0</FL><FL val=\"Quantity\">1.0</FL><FL val=\"Total\">123.0</FL><FL val=\"Discount\">1.23</FL><FL val=\"Total After Discount\">121.77</FL><FL val=\"List Price\">123.0</FL><FL val=\"Net Total\">121.77</FL></product></FL><FL val=\"Terms and Conditions\">Test by Zoho</FL><FL val=\"Description\">Test By Zoho</FL></row></SalesOrders>";
            String xmlDataString = "<SalesOrders><row no='1'><FL val=\"Subject\">百会北京专卖店</FL><FL val=\"Status\">交易成功</FL><FL val=\"Billing Country\">中国</FL><FL val=\"买家留言\">null</FL><FL val=\"Sub Total\">540.0</FL><FL val=\"Contact Name\">张立维lw</FL><FL val=\"Billing City\">北京市 海淀区</FL><FL val=\"Adjustment\">0.0</FL><FL val=\"运单号码\">null</FL><FL val=\"Grand Total\">540.0</FL><FL val=\"Description\">null</FL><FL val=\"Tax\">0</FL><FL val=\"物流公司\">null</FL><FL val=\"付款时间\">2011-11-02</FL><FL val=\"收货确认时间\">2011-11-04</FL><FL val=\"成交时间(拍下时间)\">2011-11-02</FL><FL val=\"CONTACTID\">null</FL><FL val=\"Discount\">0.0</FL><FL val=\"运费\">0.0</FL><FL val=\"Billing State\">北京</FL><FL val=\"发货方式\">free</FL><FL val=\"收件人电话\">15010424622</FL><FL val=\"Billing Code\">100080</FL><FL val=\"Due Date\">null</FL><FL val=\"Billing Street\">海淀区海淀路19-1号</FL><FL val=\"Product Details\"><product no=\"1\"><FL val=\"Unit Price\">540.0</FL><FL val=\"Product Id\">YYYYYYYYYY</FL><FL val=\"Product Name\">百会在线管理软件 维基网站开发平台 3用户版</FL><FL val=\"Discount\">0.0</FL><FL val=\"Product Description\">支持用户数:3用户</FL><FL val=\"Product Details\">null</FL><FL val=\"Quantity\">1.0</FL><FL val=\"Total\">540.0</FL><FL val=\"Tax\">0</FL><FL val=\"List Price\">540.0</FL><FL val=\"Total After Discount\">540.0</FL><FL val=\"Net Total\">540.0</FL><FL val=\"Quantity in Stock\">9998.0</FL></product></FL></row></SalesOrders>";
            System.out.println(xmlDataString.length());
            PostMethod post = new PostMethod(targetURL);
            post.setParameter("ticket", ticket);
            post.setParameter("apikey", apikey);
            post.setParameter("newFormat", newFormat);
            post.setParameter("xmlData", xmlDataString);

            HttpClient httpclient = new HttpClient();
            PrintWriter myout = null;


            // Execute http request
            try {
                long t1 = System.currentTimeMillis();
                int result = httpclient.executeMethod(post);
                System.out.println("HTTP Response status code: " + result);
                System.out.println(">> Time taken " + (System.currentTimeMillis() - t1));

                // writing the response to a file
                myout = new PrintWriter(new File("response.xml"));
                myout.print(post.getResponseBodyAsString());

                //------------Get response as a string ----------
                String postResp = post.getResponseBodyAsString();
                System.out.println("postResp=======>" + postResp);
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                myout.close();
                post.releaseConnection();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //------------------------------------Get IAM Ticket ---------------------------------
    public static String getIAMTicket(String serviceName, String loginId, String password) {
        String strTicket = null;
        try {

            String iamUrl = "https://i.baihui.com/login?servicename=" + serviceName + "&FROM_AGENT=true&LOGIN_ID=" + loginId + "&PASSWORD=" + password;
            URL u = new URL(iamUrl);
            HttpURLConnection c = (HttpURLConnection) u.openConnection();
            InputStream in = c.getInputStream();
            InputStreamReader ir = new InputStreamReader(in);
            BufferedReader br = new BufferedReader(ir);

            String strLine = null;

            while ((strLine = br.readLine()) != null) {
                if (strLine != null && strLine.startsWith("TICKET")) {
                    strTicket = strLine.substring(7);
                }
            }

            in.close();
        } catch (Exception e) {
            e.printStackTrace();

        }
        return strTicket;
    }
}
